package com.zzptc.zzsgz.bdsjws.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;

import com.github.lzyzsd.randomcolor.RandomColor;
import com.nostra13.universalimageloader.core.display.CircleBitmapDisplayer;
import com.thinkcool.circletextimageview.CircleTextImageView;
import com.zzptc.zzsgz.bdsjws.MyApplication;
import com.zzptc.zzsgz.bdsjws.R;
import com.zzptc.zzsgz.bdsjws.vo.Contact;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/6/6.
 */


public class ContactAdapter  extends RecyclerView.Adapter<ContactAdapter.MyHoldView>{
    private List<Contact> list;//所有联系人
    private List<Contact> soslist=MyApplication.getSoslist();//求救联系人
    private TextView textView;
    //int count;//全局变量系统自己初始化
    public ContactAdapter(List<Contact> list,TextView textView){
        this.list=list;
        this.textView=textView;
    }

    @Override
    public MyHoldView onCreateViewHolder(ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.show_contact_item,parent,false);
        MyHoldView myHoldView=new MyHoldView(view);
        return myHoldView;
    }

    @Override
    public void onBindViewHolder(MyHoldView holder, int position) {
        //在绑定之前要有数据
        Contact contact=list.get(position);
        holder.ctiv.setText(contact.getName().substring(1));

        holder.tv_name.setText(contact.getName());
        holder.tv_tel.setText(contact.getTelnum());
        holder.tv_addr.setText(contact.getAddress());
        //加颜色
        RandomColor randomColor=new RandomColor();
        int color=randomColor.randomColor();
        holder.ctiv.setFillColor(color);


    }

    @Override
    public int getItemCount() {
        return list.size();
    }


    class MyHoldView extends RecyclerView.ViewHolder implements View.OnClickListener {
        @BindView(R.id.cti_img)
        CircleTextImageView ctiv;
        @BindView(R.id.tv_name)
        TextView tv_name;
        @BindView(R.id.tv_tel)
        TextView tv_tel;
        @BindView(R.id.tv_addr)
        TextView tv_addr;
        @BindView(R.id.cb_contact)
        CheckBox cb_contact;

        public MyHoldView(View itemview) {
            super(itemview);
            ButterKnife.bind(this, itemview);
            //整个条目实现监听
            itemview.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            int position = getAdapterPosition();
            Contact c = list.get(position);
            String tel = c.getTelnum().replace(" ", "").replace("-","");
            //  Toast.makeText(MyApplication.getContext(), c.getName()+"==============", Toast.LENGTH_SHORT).show();
            //1  加上RecyclerView点击事件(已经解决)
            //2.  只能选三个人 多选框要选中(第一小步：点击后多选框可以选中)
            //                           (二小步：可以取消多选框)
            //                           (三小步：最多选三个联系人)
            //3. 只能选手机号
            if (tel.length()==11&&tel.matches("^1[34578]\\d{9}$")) {
                if (cb_contact.isChecked()) {
                    cb_contact.setChecked(false);
                    //count--;
                    soslist.remove(c);
                    //改变数字 难题怎样获得数组控件TextView
                    textView.setText("("+MyApplication.getSoslist().size()+")");

                } else {
                    //count只能限制在三个
                    if (soslist.size() < 3) {
                        cb_contact.setChecked(true);
                        //count++;
                        if(!soslist.contains(c)){
                            soslist.add(c);//在添加之前要判断是否已经存在了
                        }
                    } else {
                        Toast.makeText(MyApplication.getContext(), "最多只能添加三人", Toast.LENGTH_SHORT).show();
                    }
                    //改变数字
                    textView.setText("("+MyApplication.getSoslist().size()+")");
                }

            }else {
                Toast.makeText(MyApplication.getContext(), "请选择手机号码", Toast.LENGTH_SHORT).show();
            }
        }
    }
}
